﻿using HslControls;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace App.UI
{
    internal class HslLineChart : HslCurveHistory
    {
        public event EventHandler<RecordEvent> onDataMove;

        public class RecordEvent
        {
            public string[] Header { set; get; }
            public string[][] Data { set; get; }

            public RecordEvent(string[] header, string[][] data)
            {
                this.Header = header;
                this.Data = data;
            }
        }

        private float[] aspirinPrice = new float[2000];
        private float[] ibuprofenPrice = new float[2000];
        private float[] aspirinRefPrice = new float[2000];
        private float[] ibuprofenRefPrice = new float[2000];
        private int offsetPaintScrollX = 0;
        private float data_ScaleX_Render = 1f;
        private DateTime[] times = new DateTime[2000];

        public HslLineChart()
        {
            InitializeData();
            this.onCurveMouseMove += HslLineChart_onCurveMouseMove;
            this.OnScaleChanged += HslLineChart_OnChanged;
            this.OnScrollChanged += HslLineChart_OnChanged;
        }

        private void HslLineChart_OnChanged(HslCurveHistory hslCurve, int scrollX, float scale, int offsetPaintScrollX)
        {
            this.offsetPaintScrollX = offsetPaintScrollX;
            this.data_ScaleX_Render = scale;
        }

        private void HslLineChart_onCurveMouseMove(HslCurveHistory hslCurve, int x, int y)
        {
            int pos = (int)((float)(x + offsetPaintScrollX) / data_ScaleX_Render);
            if (pos >= 0)
            {
                string[] headers = new string[] { "名称", "正常价格", "参考价格" };
                string[][] values = new string[2][] {
                    new string[] {
                        "阿司匹林", 
                        aspirinPrice[pos].ToString("F2") + " 元",
                        aspirinRefPrice[pos].ToString("F2") + " 元" 
                    },
                    new string[] {
                        "布洛芬", 
                        ibuprofenPrice[pos].ToString("F2") + " 元", 
                        ibuprofenRefPrice[pos].ToString("F2") + " 元" 
                    }
                };
                onDataMove?.Invoke(this, new RecordEvent(headers, values));
            }
        }

        private void InitializeData()
        {
            Dock = DockStyle.Fill;
            Random random = new Random();

            for (int i = 0; i < aspirinPrice.Length; i++)
            {
                aspirinPrice[i] = (float)(random.NextDouble() * 30 + 20);
                ibuprofenPrice[i] = (float)(random.NextDouble() * 25 + 15);
                aspirinRefPrice[i] = (float)(random.NextDouble() * 30 + 25);
                ibuprofenRefPrice[i] = (float)(random.NextDouble() * 25 + 20);
                times[i] = DateTime.Now.AddDays(i - 2000);
            }

            SetLeftCurve("阿司匹林价格", aspirinPrice, Color.DodgerBlue, HslControls.CurveStyle.Curve, "{0:F2} 元");
            SetLeftCurve("布洛芬价格", ibuprofenPrice, Color.Green, HslControls.CurveStyle.Curve, "{0:F2} 元");
            SetRightCurve("阿司匹林参考价", aspirinRefPrice, Color.Tomato, HslControls.CurveStyle.Curve, "{0:F2} 元");
            SetRightCurve("布洛芬参考价", ibuprofenRefPrice, Color.Orange, HslControls.CurveStyle.Curve, "{0:F2} 元");
            SetDateTimes(times);
            SetCurveVisible("阿司匹林参考价", false);
            SetCurveVisible("布洛芬参考价", false);
            RenderCurveUI();
        }
    }
}
